package com.oliet.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.oliet.security.AccountUser;
import com.oliet.service.UsersService;
import com.oliet.util.CodeUtils;
import com.oliet.util.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 实体数据前置填充
 *
 * @author jiaZhenpeng
 * @date 2016/10/31
 */
@Slf4j
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {


    /**
     * 公共字段
     */
    private static final String CODE = "code";
    private static final String VERSION = "version";
    private static final String DATECREATED = "dateCreated";
    private static final String CREATEDBYCODE = "createdByCode";
    private static final String CREATEDBYNAME = "createdByName";
    private static final String LASTUPDATED = "lastUpdated";
    private static final String MODIFIEDBYCODE = "modifiedByCode";
    private static final String MODIFIEDBYNAME = "modifiedByName";
    private static final String ISDELETED = "isDeleted";

    @Autowired
    private UsersService usersService;
    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("Entity insert fill ...");
        this.strictInsertFill(metaObject, CODE, String.class, CodeUtils.getCommonCode());
        this.strictInsertFill(metaObject, VERSION, Integer.class, 0);
        this.strictInsertFill(metaObject, DATECREATED, Date.class, new Date());
        this.strictInsertFill(metaObject, LASTUPDATED, Date.class, new Date());
        this.strictInsertFill(metaObject, ISDELETED, Boolean.class, false);
        try {
            //获取当前Request
            ServletRequestAttributes ra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = ra.getRequest();
            //获取当前请求头中的token
            String token = request.getHeader("token");
            //解析token获取用户信息
            Claims claims = jwtUtils.getClaimsByToken(token);
            if (ObjectUtil.isNotNull(claims)) {
                String code = String.valueOf(claims.get("code"));
                String fullName = String.valueOf(claims.get("fullName"));
                this.strictInsertFill(metaObject, CREATEDBYCODE, String.class, code);
                this.strictInsertFill(metaObject, CREATEDBYNAME, String.class, fullName);
                this.strictInsertFill(metaObject, MODIFIEDBYCODE, String.class, code);
                this.strictInsertFill(metaObject, MODIFIEDBYNAME, String.class, fullName);
            }
        } catch (Exception e) {
            log.error("填充创建人信息失败。。。");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("Entity update fill ...");
        this.setFieldValByName(LASTUPDATED, new Date(), metaObject);
        //TODO 获取登录用户信息
        try {
            //获取当前Request
            ServletRequestAttributes ra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = ra.getRequest();
            //获取当前请求头中的token
            String token = request.getHeader("token");
            //解析token获取用户信息
            Claims claims = jwtUtils.getClaimsByToken(token);
            if (ObjectUtil.isNotNull(claims)) {
                String code = String.valueOf(claims.get("code"));
                String fullName = String.valueOf(claims.get("fullName"));
                this.setFieldValByName(MODIFIEDBYCODE, code, metaObject);
                this.setFieldValByName(MODIFIEDBYNAME, fullName, metaObject);
            }
        } catch (Exception e) {
            log.error("填充修改人信息失败。。。");

        }
    }

}
